package org.jackysoft.struts2.admin;

import java.util.Collection;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Namespace;
import org.jackysoft.annotations.Logger;
import org.jackysoft.entity.AcegiRole;
import org.jackysoft.entity.TreeNode;
import org.jackysoft.google.collection.TreeNodeZtreeFunction;
import org.jackysoft.service.TreeNodeService;
import org.jackysoft.struts2.UtilsCRUDAction;
import org.jackysoft.util.OperationType;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;

import com.google.common.base.Function;
import com.google.common.collect.Collections2;



@Namespace("/admin/treenode")
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
@Logger(label="资源URL",logType=OperationType.System)
public class TreeNodeAction extends UtilsCRUDAction<TreeNode> {

	/**
	 * 安全管理员 :不能创建账户，不能看日志
	 * 安全审计员:只能看审计日志,不能创建账户
	 * 系统管理员:只能创建账户
	 */
	private static final long serialVersionUID = 881934004853223201L;
	private TreeNodeService service;	
	private TreeNode bean;
	private Collection<TreeNode> beans;	
	private AcegiRole secRole;	
	
 	public String start() throws Exception{ 		
 		return "start";
 	}
	
   
	public String sorted() throws Exception {
		service.saveSorted(beans);
		
		return SUCCESS;
	}	

	
	public String listall() throws Exception{
		zbeans = Collections2.transform(service.getChildren(bean.getId()), new TreeNodeZtreeFunction(false));		
		return "zbeanlist";
	}	
		
	public String allbyacegi() throws Exception{
		idList = Collections2.transform(service.getChildren(secRole), new Function<TreeNode,String>(){
			
			@Override
			public String apply(TreeNode from) {
				return from.getId();
			}
			
		});
		
		return IDLIST;
	}
			
	public TreeNode getBean() {
		return bean;
	}

	public void setBean(TreeNode bean) {
		this.bean = bean;
	}

	public Collection<TreeNode> getBeans() {
		return beans;
	}

	public void setBeans(Collection<TreeNode> beans) {
		this.beans = beans;
	}

	public TreeNodeService getService() {
		return service;
	}

	@Resource
	public void setService(TreeNodeService service) {
		this.service = service;
		this.setDataProvider(service);
	}
	

	public void setSecRole(AcegiRole secRole) {
		this.secRole = secRole;
	}

	public AcegiRole getSecRole() {
		return secRole;
	}	
}